package com.liangzhong.modules.club.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.ClubEvent;
	import com.liangzhong.events.PlayerCenterEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.proxyconstant.PlayerCenter.PlayerCenterConstant;
	import com.liangzhong.proxyconstant.club.ClubProxyConstant;
	import com.lz.football.module.base.IModuleView;
	import com.lz.football.module.club.IClubView;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	
	/**
	 * 俱乐部 
	 * @author Administrator
	 * 
	 */	
	public class ClubMediator extends ModuleMediator
	{
		public static const MediatorName :String = "ClubMediator";
		
		public function ClubMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
		}
		
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get clubView():IClubView
		{
			return viewComponent as IClubView;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			AppEventDispatcher.getInstance().addEventListener(ClubEvent.CHAGELOGO,onChangeLogo);
			AppEventDispatcher.getInstance().addEventListener(ClubEvent.CHANGESHIRT,onChangeshirt);
			AppEventDispatcher.getInstance().addEventListener(ClubEvent.REMOVE_MODE,onremoveMode);
			AppEventDispatcher.getInstance().addEventListener(PlayerCenterEvent.ADD_MORALE,onAddMoraleHandler);
			AppEventDispatcher.getInstance().addEventListener(ClubEvent.CHANGE_NATION,onchangeNationHandler);
		}
		
		protected function onchangeNationHandler(event:ClubEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(ClubProxyConstant.CLUB_MEDIATOR_NOTIFICATION,event.param,ClubProxyConstant.CHANGENATION);
		}
		
		protected function onremoveMode(event:Event):void
		{
//			removeMediator();
			facade.removeMediator(MediatorName);
		}
		
		/**
		 *更换队徽 
		 * @param event
		 * 
		 */
		protected function onChangeLogo(event:ClubEvent):void
		{
			this.sendNotification(ClubProxyConstant.CLUB_MEDIATOR_NOTIFICATION,event.param,ClubProxyConstant.CHANGELOGO);
		}
		/**
		 *更换球服 
		 * @param evnet
		 * 
		 */
		private function onChangeshirt(event:ClubEvent):void
		{
			this.sendNotification(ClubProxyConstant.CLUB_MEDIATOR_NOTIFICATION,event.param,ClubProxyConstant.CHANGESHIRT);
		}
		/**
		 * 删除事件 
		 * 
		 */
		override public function removeEventListeners():void
		{
			AppEventDispatcher.getInstance().removeEventListener(ClubEvent.CHAGELOGO,onChangeLogo);
			AppEventDispatcher.getInstance().removeEventListener(ClubEvent.CHANGESHIRT,onChangeshirt);
			AppEventDispatcher.getInstance().removeEventListener(ClubEvent.REMOVE_MODE,onremoveMode);
			AppEventDispatcher.getInstance().removeEventListener(PlayerCenterEvent.ADD_MORALE,onAddMoraleHandler);
			AppEventDispatcher.getInstance().removeEventListener(ClubEvent.CHANGE_NATION,onchangeNationHandler);
			
		}
		/**
		 *增加士气  
		 * @param event
		 * 
		 */
		protected function onAddMoraleHandler(event:PlayerCenterEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(PlayerCenterConstant.PLAYERCENTER_MEDIATOR_NOTIFICATION,
				event.param,PlayerCenterConstant.ADD_MORMAL);
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [ClubProxyConstant.GET_CHANGELOGO,
				ClubProxyConstant.GET_CHANGESHIRT,
				ClubProxyConstant.ECONOMY_VALUE_CHANGE,
				PlayerCenterConstant.GET_ADD_MORMAL,
				ClubProxyConstant.GET_CHANGENATION].concat(super.listNotificationInterests());
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ClubProxyConstant.GET_CHANGELOGO:
					clubView.getChangeLogo(notification.getBody());
					break;
				case ClubProxyConstant.GET_CHANGESHIRT://修改球衣
					clubView.getChangeShirt(notification.getBody());
					break;
				case ""://获得一件新球衣
					clubView.getOntShirt(notification.getBody());
					break;
				case ClubProxyConstant.ECONOMY_VALUE_CHANGE:
					clubView.economyvaluechange(notification.getBody());
					break;
				case PlayerCenterConstant.GET_ADD_MORMAL:
					clubView.addMornal(notification.getBody());
					break;
				case ClubProxyConstant.GET_CHANGENATION://更换国家
					clubView.getChangeNation(notification.getBody());
					break;
				
			}
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			resName = ResourceConst.CLUB_RESOURCE;
			if(ResourceManager.instance.isContainResource(ResourceConst.CLUB_RESOURCE))
			{
				showUI();
			}
			else
			{
				loader = new QueueLoader();
				var clubConfig :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).club[0];
				bytesTotal = int(clubConfig.@totalBytes);
				var releaseVersion :String;
				for each(var item :XML in clubConfig.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				loadRes();
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 * 
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			showUI();
			super.onLoadComplete(e);
			ResourceManager.instance.putResource(ResourceConst.CLUB_RESOURCE,resList)
		}
		private function showUI():void
		{
			var cView :IModuleView = Reflection.createInstance("com.lz.football.module.club.ClubView");
			cView.initUI();
			viewComponent = cView;
			GlobalDef.GameWindowContainer.addChild(viewComponent as DisplayObject);
			addEventListeners();
			//设置缓存位图
			setCacheAsBitmap(true);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			(viewComponent as IModuleView).destroyView();
			viewComponent = null;
		}
	}
}
//
///**
// *获取 实例 
// * @return 
// * 
// */		
//public function get clubView():DisplayObjectContainer
//{
//	return viewComponent as DisplayObjectContainer;
//}
///**
// *添加事件 
// * 
// */		
//override public function addEventListeners():void
//{
//	
//}
///**
// * 删除事件 
// * 
// */
//override public function removeEventListeners():void
//{
//	
//}
///**
// * 监听信息
// * @return 
// * 
// */		
//override public function listNotificationInterests():Array
//{
//	return [].concat(super.listNotificationInterests());
//}
///**
// * 处理信息
// * @param notification
// * 
// */		
//override public function handleNotification(notification:INotification):void
//{
//	switch(notification.getName())
//	{
//		
//		
//	}
//}
///**
// * 
// * 注册
// */		
//override public function onRegister():void
//{
//	resName = ResourceConst.CLUB_RESOURCE;
//	if(ResourceManager.instance.isContainResource(ResourceConst.CLUB_RESOURCE))
//	{
//		showUI();
//	}
//	else
//	{
//		loader = new QueueLoader();
//		var clubConfig :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).club[0];
//		var releaseVersion :String;
//		for each(var item :XML in clubConfig.item)
//		{
//			releaseVersion = "?" + item.@version + "." + item.@itemType;
//			loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
//		}
//		loadRes();
//	}
//}
///**
// *加载资源完毕 
// * @param e
// * 
// */		
//override protected function onLoadComplete(e:QueueLoaderEvent):void
//{
//	showUI();
//	super.onLoadComplete(e);
//	ResourceManager.instance.putResource(ResourceConst.CLUB_RESOURCE,resList)
//}
//private function showUI():void
//{
//	var cView :IModuleView = Reflection.createInstance("com.lz.football.module.club.ClubView");
//	cView.initUI();
//	viewComponent = cView;
//	GlobalDef.GameContainer.addChild(viewComponent as DisplayObject);
//	addEventListeners();
//}
///**
// * 删除 mediator
// * 
// */		
//override public function removeMediator():void
//{
//	facade.removeMediator(MediatorName);
//}
///**
// *删除 清理 
// * 
// */		
//override public function onRemove():void
//{
//	super.onRemove();
//	removeEventListeners();
//	(viewComponent as IModuleView).destroyView();
//	viewComponent = null;
//}